<template>
  <el-dialog
    :title="!dataForm.id ? $t('Common.Add') : $t('Common.Edit')"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="菜单" prop="fatherId">
      <!-- <el-input v-model="dataForm.fatherId" placeholder="父级（0父级）"></el-input> -->
      <el-select filterable  v-model="dataForm.fatherId" placeholder="请选择">
        <el-option-group
          v-for="group in options"
          :key="group.id"
          :label="group.name">
          <el-option
            v-for="item in group.children"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-option-group>
      </el-select>
    </el-form-item>
    <el-form-item label="内容" prop="content">
      <el-input
        type="textarea"
        :rows="3"
        placeholder="请输入模板内容"
        v-model="dataForm.content">
      </el-input>
    </el-form-item>

    <el-form-item label="排序" prop="sort">
      <el-input-number v-model="dataForm.sort" :min="0" label="排序"></el-input-number>
    </el-form-item>
    <el-form-item label="状态" prop="state">
        <el-radio v-model="dataForm.state" :label="1">启用</el-radio>
      <el-radio v-model="dataForm.state" :label="0">禁用</el-radio>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('Common.Cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ $t('Common.Confirm') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        props:{
          expandTrigger: 'hover',
          value: 'id',
          label: 'name',
        },
        value: [],
        options: [],
        visible: false,
        dataForm: {
          id: 0,
          sysUserId: '',
          fatherId: '',
          content: '',
          sort: 0,
          state: 1
        },
        dataRule: {
          fatherId: [
            { required: true, message: '上级菜单不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '内容不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      menu(){
        this.$http({
          url: this.$http.adornUrl(`/hotel/thotelsmstemplatemenu/joint_search`),
          method: 'get',
          params: this.$http.adornParams({
            'page': 0,
            'limit': 999,
            'father_id': 0
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.options = data.page.list
          }
        })
      },
      handleChange(value) {
        this.dataForm.fatherId = value[1]
      },
      init (id) {
        this.menu()
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelsmstemplatecontent/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.sysUserId = data.tHotelSmsTemplateContent.sysUserId
                this.dataForm.fatherId = data.tHotelSmsTemplateContent.fatherId
                this.dataForm.content = data.tHotelSmsTemplateContent.content
                this.dataForm.sort = data.tHotelSmsTemplateContent.sort
                this.dataForm.state = data.tHotelSmsTemplateContent.state
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelsmstemplatecontent/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'sysUserId': this.dataForm.sysUserId,
                'fatherId': this.dataForm.fatherId,
                'content': this.dataForm.content,
                'sort': this.dataForm.sort,
                'state': this.dataForm.state
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: this.$t('Common.OperationSuccessful'),
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
